package com.zsc.externalservice.services.impl;

import com.zsc.externalservice.entity.AdsSjzlXsSalOrderdtl;
import com.zsc.externalservice.entity.vo.OrderVO;
import com.zsc.externalservice.mapper.OrderMapper;
import com.zsc.externalservice.services.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 获取用户的所有订单信息
     *
     * @param custUuid 用户uuid
     * @return List
     */
    @Override
    public List<OrderVO> getAllOrderList(String custUuid) {
        List<AdsSjzlXsSalOrderdtl> list = orderMapper.getByCustUuid(custUuid, null);
        List<OrderVO> result = new ArrayList<>();
        if (list.isEmpty()) {
            return null;
        }
//        log.warn("获取到的数据是：{}", list.get(0));
        for (AdsSjzlXsSalOrderdtl a : list) {
            OrderVO orderVO = OrderVO.builder()
                    .date(a.getBizDate())
                    .orderUuid(a.getOrderUuid())
                    .sumQty(a.getSumQty())
                    .sumWithTaxAmt(a.getSumWithtaxamt())
                    .qty(a.getQty())
                    .taxPrice(a.getTaxprice())
                    .withTaxAmt(a.getWithtaxamt())
                    .build();
            result.add(orderVO);
        }
        return result;
    }

    /**
     * 根据用户uuid获取最近一次订单的卷烟数据
     * @param customUuid 用户uuid
     * @return List
     */
    @Override
    public List<OrderVO> getOrderListByCustUuid(String customUuid) {
        //获取用户最近一次下单日期
        LocalDate date = orderMapper.getLastOrderDate(customUuid).getDate();
        log.info("最近一次下单日期是：{}", date);

        //处理数据
        List<AdsSjzlXsSalOrderdtl> list = orderMapper.getByCustUuid(customUuid, date);
        List<OrderVO> result = new ArrayList<>();
        for (AdsSjzlXsSalOrderdtl a : list) {
            OrderVO orderVO = OrderVO.builder()
                    .date(a.getBizDate())
                    .orderUuid(a.getOrderUuid())
                    .sumQty(a.getSumQty())
                    .sumWithTaxAmt(a.getSumWithtaxamt())
                    .qty(a.getQty())
                    .taxPrice(a.getTaxprice())
                    .withTaxAmt(a.getWithtaxamt())
                    .productCode(a.getProductCode())
                    .build();
            result.add(orderVO);
        }
        return result;
    }
}
